﻿using System;
using Company.BLL;
using Company.BLL.KDT.GC;
using Company.BLL.Utils;
using Company.BLL.DuLieuChuan;
using System.Data;
using Janus.Windows.GridEX;

namespace Company.Interface.KDT.GC.PCTFormTemplate
{
    public partial class FormBoSungNhomSP : BaseForm
    {
        public Company.BLL.KDT.GC.PhuKienDangKy pkdk = new Company.BLL.KDT.GC.PhuKienDangKy();
        public Company.BLL.KDT.GC.PhuKien pk = new Company.BLL.KDT.GC.PhuKien();
        public Company.BLL.KDT.GC.HopDong hopdong = new Company.BLL.KDT.GC.HopDong();
       
        public bool isAdd = true;
        private string dvt = string.Empty;
        private int STTHang = 0;
        public FormBoSungNhomSP()
        {
            InitializeComponent();           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            //this.Close();
        }

        private void LoadLoaiSanPham()
        {
            cbTen.DataSource = Company.BLL.DuLieuChuan.NhomSanPham.SelectAll().Tables[0];
            cbTen.DisplayMember = "TenSanPham";
            cbTen.ValueMember = "MaSanPham";
            this._DonViTinh = DonViTinh.SelectAll();
            cbDonViTinh.DataSource = this._DonViTinh;
            cbDonViTinh.DisplayMember = "Ten";
            cbDonViTinh.ValueMember = "ID";
        }
        private void LoaiSanPhamGCEditForm_Load(object sender, EventArgs e)
        {
            LoadLoaiSanPham();                       
            if (!isAdd)
            {
                cbTen.SelectedValue = pk.MaHang.Trim();
                txtSoLuong.Text = pk.SoLuong.ToString().Trim();
                txtGia.Text = pk.DonGia.ToString().Trim();
                cbDonViTinh.SelectedValue = pk.DVT_ID;
                btnAdd.Text = "Sửa";
            }
            if (pkdk.TrangThaiXuLy == TrangThaiXuLy.DA_DUYET)
            {
                btnAdd.Enabled = false;                
            }
            dgList.DataSource = pkdk.PKCollection;
            foreach (PhuKien s in pkdk.PKCollection)
            {
                if (s.STTHang > STTHang)
                    STTHang = s.STTHang;
            }
        }
        private void reset()
        {
            txtGia.Text = "0.000";
            txtSoLuong.Text = "0.000";
            //STTHang++;            
            cbTen.Text = "";
            dgList.Refetch();
            cbTen.Focus();
            isAdd = true;
            pk = new PhuKien();
            btnAdd.Text = "Thêm";  
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {            
            cvError.Validate();           
            if (!cvError.IsValid) return;
            {
                if (Convert.ToDecimal(txtSoLuong.Text) == 0)
                {
                    error.SetError(txtSoLuong, "Số lượng phải lớn hơn 0.");
                    return;
                }
                if (Convert.ToDecimal(txtGia.Text) == 0)
                {
                    error.SetError(txtGia, "Giá gia công phải lớn hơn 0.");
                    return;
                }
                            
                checkExitsNhomSanPhamAndSTTHang();                
            }
           
            
        }
        private void checkExitsNhomSanPhamAndSTTHang()
        {
            if (!isAdd)
                pkdk.PKCollection.Remove(pk);
            else//lay thong tin tao moi
            {
                pk.STTHang = STTHang + 1;
                pk.SoLuong = Convert.ToDecimal(txtSoLuong.Text);
                pk.DonGia = Convert.ToDecimal(txtGia.Text);
                pk.TenHang = cbTen.Text.Trim();
                pk.MaHang = cbTen.SelectedValue.ToString();
                pk.DVT_ID = cbDonViTinh.SelectedValue.ToString();
            }
           
                 
            foreach (PhuKien p in pkdk.PKCollection)
            {
                if (p.MaHang.Trim()==cbTen.SelectedValue.ToString().Trim())
                {
                    if (isAdd)//cap nhat
                    {
                        string st = ShowMessage("Đã có sản phẩm này trong danh sách. Bạn có muốn cập nhật lại không?", true);
                        if (st == "Yes")
                        {                           
                            p.SoLuong = Convert.ToDecimal(txtSoLuong.Text);
                            p.DonGia = Convert.ToDecimal(txtGia.Text);                           
                            p.MaHang = cbTen.SelectedValue.ToString();
                            p.TenHang = cbTen.Text.Trim();
                            p.DVT_ID = cbDonViTinh.SelectedValue.ToString();
                            reset();                          
                            return ;
                        }
                        else
                        {
                            pk = new PhuKien();
                            cbTen.Focus();
                            return ;
                        }
                    }
                    else
                    {
                        ShowMessage("Đã có sản phẩm này trong danh sách.", false);
                        pkdk.PKCollection.Add(pk);
                        cbTen.Focus();
                        return;
                    }
                }                                  
            }
            if (isAdd)
            {
                pkdk.PKCollection.Add(pk);
                STTHang++;
                reset();
            }
            else//cap nhat
            {
                pk.SoLuong = Convert.ToDecimal(txtSoLuong.Text);
                pk.DonGia = Convert.ToDecimal(txtGia.Text);
                pk.MaHang = cbTen.SelectedValue.ToString();
                pk.TenHang = cbTen.Text.Trim();
                pk.DVT_ID = cbDonViTinh.SelectedValue.ToString();
                pkdk.PKCollection.Add(pk);
                reset();                                         
            }            
        }
        private void dgNhomSanPham_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {
            pk = new PhuKien();
            pk.MaHang = e.Row.Cells[3].Text.Trim();
            pk.TenHang = e.Row.Cells[4].Text.Trim();
            pk.DVT_ID = e.Row.Cells[5].Text;
            pk.SoLuong = Convert.ToDecimal(e.Row.Cells[7].Text);
            pk.ID = (long)Convert.ToDecimal(e.Row.Cells[2].Text);
            pk.STTHang = (int)Convert.ToDecimal(e.Row.Cells[1].Text);
            pk.Master_ID = pkdk.ID;
            isAdd = false;
            btnAdd.Text = "Sửa";
            cbDonViTinh.SelectedValue = pk.DVT_ID;
            cbTen.SelectedValue = pk.MaHang.Trim();
            txtGia.Text = pk.DonGia.ToString();
            txtSoLuong.Text = pk.SoLuong.ToString();
        }

      

        private void dgNhomSanPham_DeletingRecord(object sender, Janus.Windows.GridEX.RowActionCancelEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa nhóm sản phẩm này không?", true) == "Yes")
                {
                    PhuKien pkDelete = new PhuKien();
                    pkDelete.ID = (long)Convert.ToInt64(e.Row.Cells[2].Text);
                    if (pkDelete.ID > 0)
                    {
                        pkDelete.Delete();                    
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells[6].Text = this.DonViTinh_GetName(e.Row.Cells[5].Value).Trim();            
        }

       

       

    }
}